import React, { Component } from 'react';
import { connect } from 'dva';
import { message, Modal, Pagination, Popconfirm, Button } from 'antd';
import styles from './../Users.css';
import moment from 'moment';
import Factory from '../../factory';
import numeral from 'numeral';
import * as datetime from '../../../utils/datetime'
import dataTableCreate from '../../addons/DataTable'
import dataImportCreate from '../../addons/DataImport'
import * as menuService from '../../../services/menus'
import * as com from '../../../utils/common';
const daoName = "INDI_LOAN";
const daoNameImp = "IndiLoanJydw";
const ButtonGroup = Button.Group; 
const renderNumber = (num) => {
  let value = ''
  if (num) {
    value = numeral(num).format('0,0')
  }
  return value
};
const columns = [
    { title: '借据号', dataIndex: 'Jjh', key: 'Jjh', width: 140, sorter: true, dispaly: true, filter: true, export: true },
    //{ title: '经营单位', nowrap: true, caption: "Jydw", key: 'Jydw', width: 120, sorter: true, dispaly: true, filter: true, export: true },
    { title: '客户名称', dataIndex: 'Khmc', key: 'Khmc', width: 120, sorter: true, dispaly: true, filter: true },
    { title: '身份证号', dataIndex: 'Idno', key: 'Idno', width: 210, sorter: true, dispaly: true, filter: true },
    { title: '起息日', width: 100, sorter: true, key: 'Qxr', caption: "Qxr", render: data => datetime.format(data.Qxr, "yyyy-MM-dd"), dispaly: true, filter: true, canEdit: true },
    { title: '到期日', width: 100, sorter: true, key: 'Dqr', caption: "Dqr", render: data => datetime.format(data.Dqr, "yyyy-MM-dd"), dispaly: true, filter: true, canEdit: true },
    { title: '发放金额', className: 'column-money', caption: "Ffje", render: data => com.formatNumber(data.Ffje), key: 'Ffje', width: 150, sorter: true, dispaly: true, filter: true },
    { title: '基准利率', className: 'column-money', caption: "Jzll", render: data => com.formatNumber(data.Jzll), key: 'Jzll', width: 120, sorter: true, dispaly: true, filter: true },
    { title: '开户网点', dataIndex: 'Khwd', key: 'Khwd', width: 130, sorter: true, dispaly: true, filter: true },
    { title: '贷款类型', dataIndex: 'Dklx', key: 'Dklx', width: 140, sorter: true, dispaly: true, filter: true },
    { title: '当期数', width: 100, sorter: true, dataIndex: 'Dqs', key: 'Dqs', dispaly: true, filter: true },
    { title: '浮动利率', width: 110, sorter: true, dataIndex: 'Fdll', key: 'Fdll', dispaly: true, filter: true },
    { title: '客户经理1', width: 180, sorter: false, dataIndex: 'YG1', key: 'YG1', dispaly: true, filter: false },
    { title: '分配比例1(%)', width: 100, sorter: false, className: "column-money", caption:'ZB1',render: data => renderNumber(data.ZB1), key: 'ZB1', dispaly: true, filter: false },
    { title: '客户经理2', width: 180, sorter: false, dataIndex: 'YG2', key: 'YG2', dispaly: true, filter: false },
    { title: '分配比例2(%)', width: 100, sorter: false, className: "column-money", caption:'ZB2',render: data => renderNumber(data.ZB2), key: 'ZB2', dispaly: true, filter: false },
    { title: '客户经理3', width: 180, sorter: false, dataIndex: 'YG3', key: 'YG3', dispaly: true, filter: false },
    { title: '分配比例3(%)', width: 100, sorter: false, className: "column-money", caption:'ZB3',render: data => renderNumber(data.ZB3), key: 'ZB3', dispaly: true, filter: false },
    { title: '客户经理4', width: 180, sorter: false, dataIndex: 'YG4', key: 'YG4', dispaly: true, filter: false },
    { title: '分配比例4(%)', width: 100, sorter: false, className: "column-money", caption:'ZB4',render: data => renderNumber(data.ZB4), key: 'ZB4', dispaly: true, filter: false },
    { title: '客户经理5', width: 180, sorter: false, dataIndex: 'YG5', key: 'YG5', dispaly: true, filter: false },
    { title: '分配比例5(%)', width: 100, sorter: false, className: "column-money", caption:'ZB5',render: data => renderNumber(data.ZB5), key: 'ZB5', dispaly: true, filter: false },
];
const controls = [
    { name: 'Jydw', required: true, type: 'text', label: '经营单位', grid: 12 / 24 },
    { name: 'Khmc', required: true, type: 'text', label: '客户名称', grid: 12 / 24 },
    { name: 'Idno', required: true, type: 'text', label: '身份证号', grid: 12 / 24 },
    { name: "Qxr", label: "起息日", type: "date", grid: 12 / 24, dataType: "date" },
    { name: "Dqr", label: "到期日", type: "date", grid: 12 / 24, dataType: "date" },
    { name: 'Ffje', required: true, type: 'number', label: '发放金额', grid: 12 / 24 },
    { name: 'Jzll', required: true, type: 'number', label: '基准利率', grid: 12 / 24 },
    { name: 'Khwd', required: true, type: 'text', label: '开户网点', grid: 12 / 24 },
    { name: 'Jjh', required: true, type: 'text', label: '借据号', grid: 12 / 24 },
    { name: 'Dklx', required: true, type: 'text', label: '贷款类型', grid: 12 / 24 },
    { name: "Dqs", label: "当期数", type: "number", grid: 12 / 24 },
    { name: "Fdll", label: "浮动利率", type: "number", grid: 12 / 24 },
]
const controlsImp = [
    {
        name: 'JJH', required: true, type: 'search', label: '借据号', grid: 12 / 24,
        fetch: { daoName: "INDI_LOAN", searchColumns: ['Jjh'], optionTpl: 'Jjh', valueTpl: "Jjh" }
    },
    {
        name: 'JYDW', required: true, type: 'search', label: '经营单位', grid: 12 / 24,
        fetch: { daoName: "Organization", searchColumns: ['Name'], optionTpl: 'Name', valueTpl: "Name" }
    },
]
const DataTable = dataTableCreate(daoName);
const DataImport = dataImportCreate(daoNameImp);
class INDI_LOANS extends Component {
    constructor(props) {
        super(props);
        this.state = {
            dataChange: false
        };
    }
    componentDidMount() {
        this.props.dispatch({
            type: `${daoName}/setKeyName`,
            payload: { keyName: "ID", permID: this.props.permID }
        });
        this.props.dispatch({
            type: `${daoName}/fetch`,
            payload: { daoName, page: 1, pageSize: 20 }
        });
        this.loadOptionData();
    }
    componentWillReceiveProps(nextProps) {
        const { showImport } = this.props
        if (!nextProps.showImport && showImport) {
            this.props.dispatch({
                type: `${daoName}/fetch`,
                payload: { daoName, page: 1, pageSize: 20 }
            });
        }
    }
    loadOptionData() {
        let options = []
        controls.map(c => {
            if (c.fetch) {
                let f = c.fetch;
                if (c.type === 'cascader-select') {
                    f.isCascader = true;
                }
                if (c.type !== 'search') {
                    options.push(f);
                }
            }
        })
        if (daoName && options.length > 0) {
            this.props.dispatch({ type: `${daoName}/fetchOptions`, payload: { options } });
        }
    }
    showImportHandler() {
        this.props.dispatch({
            type: `${daoNameImp}_DataImport/setDisplay`,
            payload: { display: true },
        });
    }
    DownTmportHandler() {
        let columnHeaders = []
        columns.map((c, j) => {
            if (!c.export) {
                return
            }
            let header = {
                Name: c.key,
                Caption: c.title,
                Width: c.width
            }
            columnHeaders.push(header)
        })
        let form = document.createElement('form');//定义一个form表单
        let whereItems = [];
        whereItems.push({
            FieldName: "1",
            Operator: "<>",
            FieldValues: "1"
        })
        const whereCauses = []
        whereCauses.push({
            WhereItems: whereItems,
            RelationSign: "AND"
        })
        const orderItems = []
        form.style = "display:none";
        form.target = "_blank";
        form.method = "post";
        form.action = "/api/ExportXlsx/" + daoName;
        let input1 = document.createElement("input");
        input1.type = "hidden";
        input1.name = "WhereCauses";
        input1.value = JSON.stringify(whereCauses);
        let input2 = document.createElement("input");
        input2.type = "hidden";
        input2.name = "OrderItems";
        input2.value = JSON.stringify(orderItems)
        let input3 = document.createElement("input");
        input3.type = "hidden";
        input3.name = "ColumnHeaders";
        input3.value = JSON.stringify(columnHeaders);
        let input4 = document.createElement("input");
        input4.type = "hidden";
        input4.name = "PermID";
        input4.value = "" + this.props.permID;
        let input5 = document.createElement("input");
        input5.type = "hidden";
        input5.name = "UserID";
        input5.value = "" + this.props.userID;
        document.body.appendChild(form);//将表单放置在web中
        form.appendChild(input1);
        form.appendChild(input2);
        form.appendChild(input3);
        form.appendChild(input4);
        form.appendChild(input5);
        form.submit();
        document.body.removeChild(form)
    }
    render() {
        let { showEdit, record, permID, popedomTypes } = this.props
        const pTypes = popedomTypes[permID]
        let data = null
        if (pTypes.indexOf("2") !== -1) {
            data = <ButtonGroup style={{ zIndex: 0 }}>
                {/* <Button onClick={this.showImportHandler.bind(this)}>经营单位导入</Button> */}
                <Button onClick={this.DownTmportHandler.bind(this)}>下载导入模板</Button>
            </ButtonGroup>
        }
        return (
            <div className={styles.normal} style={{ background: '#fff', marginTop: -16, padding: '5px 5px' }}>
                <div>
                    <div className={styles.create}>
                        {data}
                    </div>
                    <DataTable ref="userTable" daoName={daoName} columns={columns} controls={controls} permID={permID} />
                    <DataImport title="经营单位" importName="IndiLoanJydw" daoName="IndiLoanJydw" keys={["ID"]} importMode="TruncteAfterInsert" controls={controlsImp} />
                </div>
            </div>
        )
    }
}
function mapStateToProps(state) {
    const { showEdit, record, selectedRowKeys, keyName } = state[daoName];
    const { userID, orgID, pOrgID, popedomTypes } = state.menus;
    const { display } = state[`${daoNameImp}_DataImport`];
    return {
        userID,
        orgID,
        pOrgID,
        popedomTypes,
        showEdit,
        selectedRowKeys,
        keyName,
        record,
        showImport: display
    }
}
INDI_LOANS = connect(mapStateToProps)(INDI_LOANS)
export default INDI_LOANS;
let render = (props) => React.createElement(INDI_LOANS, props);
let f = new Factory()
f.set("INDI_LOANS", render)